package system

import (
	"encoding/json"
	"ltbz/cultural_tourism_api/controllers"
	"ltbz/cultural_tourism_api/models"
	"ltbz/cultural_tourism_api/models/system"
	systemServices "ltbz/cultural_tourism_api/services/system"
	"ltbz/cultural_tourism_api/utils"
)

type SysRoleController struct {
	controllers.BaseAuthController
}

// @Title 添加角色
// @Description 添加角色
// @Param	request	body system.SysRoleAddReq true "type json string"
// @Success 200
// @router /role/add [post]
func (this *SysRoleController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysRoleAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.RoleName == "" {
		br.Msg = "角色名称不能为空"
		return
	}

	err, errMsg := systemServices.AddRole(&req)
	if err != nil {
		br.ErrMsg = "保存失败，Err:" + err.Error() + ";errMsg:" + errMsg
		br.Msg = "保存失败"
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 编辑角色菜单权限
// @Description 编辑角色菜单权限
// @Param	request	body system.SysRoleMenuEditReq true "type json string"
// @Success 200
// @router /role/menu/edit [post]
func (this *SysRoleController) EditMenu() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysRoleMenuEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	err, errMsg := systemServices.EditRoleMenu(&req)
	if err != nil {
		br.Msg = "编辑失败！"
		br.ErrMsg = "编辑失败，Err:" + err.Error() + ";Err:" + errMsg
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 获取角色列表
// @Description 获取角色列表
// @Param	request	body system.SysRolePageResp true "type json string"
// @Success 200
// @router /role/list [post]
func (this *SysRoleController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	body := this.Ctx.Input.RequestBody
	req := new(system.SysRoleListReq)
	if string(body) != "" {
		err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
		if err != nil {
			br.Msg = "参数解析异常！"
			br.ErrMsg = "参数解析失败，Err:" + err.Error()
			return
		}
	}

	if req.PageSize <= 0 {
		req.PageSize = utils.PageSize20
	}
	if req.CurrentIndex <= 0 {
		req.CurrentIndex = 1
	}

	startSize := utils.StartIndex(req.CurrentIndex, req.PageSize)

	item := new(system.SysRole)

	var condition string
	var pars []interface{}

	if req.RoleName != "" {
		roleName := `%` + req.RoleName + `%`
		condition += "AND role_name LIKE ?"
		pars = append(pars, roleName)

	}

	total, err := item.GetSysRolePageCount(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	list, err := item.GetSysRolePage(condition, pars, startSize, req.PageSize)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	page := utils.GetPaging(req.CurrentIndex, req.PageSize, total)
	resp := new(system.SysRolePageResp)
	resp.List = list
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp.Paging = page

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = resp
	return
}

// @Title 获取角色菜单权限
// @Description 获取角色菜单权限
// @Param	request	body system.SysRoleMenuListReq true "type json string"
// @Success 200
// @router /role/menu/list [post]
func (this *SysRoleController) MenuList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysRoleMenuListReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	list, err, errMsg := systemServices.RoleMenuList(&req)
	if err != nil {
		br.Msg = "获取失败！"
		br.ErrMsg = "获取失败，Err:" + err.Error() + ";Err:" + errMsg
		return
	}
	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = list
	return
}

// @Title 删除角色
// @Description 删除角色
// @Param	request	body system.SysRoleDelReq true "type json string"
// @Success 200
// @router /role/delete [post]
func (this *SysRoleController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysRoleDelReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.RoleId <= 0 {
		br.Msg = "参数错误"
		return
	}
	err, errMsg := systemServices.DeleteRole(&req)
	if err != nil {
		br.Msg = "删除失败！"
		br.ErrMsg = "删除失败，Err:" + err.Error() + ";errMsg:" + errMsg
		return
	}
	br.Msg = "删除成功！"
	br.Ret = 200
	return
}

// @Title 根据角色获取系统用户
// @Description 根据角色获取系统用户
// @Param	request	body system.SysRoleUserListReq true "type json string"
// @Success 200
// @router /role/user/list [post]
func (this *SysRoleController) RoleUserList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	body := this.Ctx.Input.RequestBody
	req := new(system.SysRoleUserListReq)
	if string(body) != "" {
		err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
		if err != nil {
			br.Msg = "参数解析异常！"
			br.ErrMsg = "参数解析失败，Err:" + err.Error()
			return
		}
	}

	if req.RoleName == "" {
		br.Msg = "角色名称不能为空"
		return
	}

	item := new(system.SysRole)

	var condition string
	var pars []interface{}

	if req.RoleName != "" {
		condition += " AND b.role_name = ? "
		pars = append(pars, req.RoleName)
	}
	list, err := item.SysRoleUserListPage(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = list
	return
}

// @Title 获取所有角色
// @Description 获取所有角色
// @Param	request	body system.SysRoleAllReq true "type json string"
// @Success 200
// @router /role/all [post]
func (this *SysRoleController) All() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	body := this.Ctx.Input.RequestBody
	req := new(system.SysRoleAllReq)
	if string(body) != "" {
		err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
		if err != nil {
			br.Msg = "参数解析异常！"
			br.ErrMsg = "参数解析失败，Err:" + err.Error()
			return
		}
	}

	item := new(system.SysRole)

	var condition string
	var pars []interface{}

	if req.RoleName != "" {
		roleName := "'%" + req.RoleName + "%'"
		condition += " role_name like ?"
		pars = append(pars, roleName)
	}
	list, err := item.GetSysRoleAll(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = list
	return
}
